#ifndef _COMMUNICATION_H_
#define _COMMUNICATION_H_

#include <unistd.h>
#include <pthread.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include <fcntl.h>
#include <math.h>
#include <algorithm>

#include "glog/logging.h"
#include "DB_MySql.h"
#include "ProcessOrder.h"
#include "TypeFactory.h"

#define ASK 0
#define ID  1
#define OFF 2
#define ON  3
#define ACK 4
#define MOVE 5
#define REJECT   6
#define ORDEROK   7

using namespace std;
int  GetValue(char arr[], int PosBegin,int Length);

BOOL SocketSend(int new_fd ,char SendBuf[],int Length);
BOOL SocketReceive(int new_fd , char ReadBuf[],int Length);

short AskRole(int new_fd);
void Commynication(int p);

BOOL CommunicateWithUser(int new_fd);
BOOL CommunicateWithDevice(int new_fd);

extern vector<UserNode>   UserOnlineList;
extern vector<DeviceNode> DeviceOlineList;

string strEraseAll(string str ,char chOut );
int  GetfdFromList(string DeviceID,vector<DeviceNode> DeviceList);
void CommunicationInit();
void CommunicationDeInit();
int  GetOrderType(char recv[]);
BOOL ProcessLogin(int new_fd ,char recv[]);
BOOL ProcessConDev(int new_fd ,char recv[]);
BOOL ProcessCurState(int new_fd ,char recv[]);
BOOL ProcessCurMove(int new_fd ,char recv[]);

vector<string> MySplit(string v_Str, char v_chDelimiter);
#endif
